package com.irdstudio.apicenter.gateway.util;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;

/**
 * 读取配置文件的工具类
 * @author luyang
 *
 */
public class ConfigUtil {
	static private Properties dbProps = new Properties();
	public static String URL;
	static {
		loads();
	}
	

	public static String getURL() {
		return dbProps.getProperty("url");
	}
	public static String getApiId() {
		return dbProps.getProperty("apiId");
	}
	public static String getApiSecret() {
		return dbProps.getProperty("apiSecret");
	}
	

	/**
	 * 加载配置文件
	 */
	synchronized static public void loads() {
		if (dbProps.size() == 0) {
			InputStream is = ConfigUtil.class.getResourceAsStream("/sys_config.properties");

			try {
				dbProps.load(is);
				for (int i = 0; i < dbProps.size(); i++) {
					dbProps.keySet();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 重新加载配置文件
	 */
	synchronized static public void reLoads() {
		String path = ConfigUtil.class.getProtectionDomain().getCodeSource().getLocation().getPath();
		String rootPath = path.substring(0, path.lastIndexOf("/classes"));  
	    String propertyFilePath = rootPath + "/classes/sys_config.properties";
        FileInputStream fis;  
        try {  
            fis = new FileInputStream(propertyFilePath);
            dbProps.load(fis);
			for (int i = 0; i < dbProps.size(); i++) {
				dbProps.keySet();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
